package b4.widget.tab
{
	import b4.util.basic.*;
	import b4.widget.basic.*;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.*;
	import flash.text.*;
	import flash.ui.Mouse;

	public class TabController extends BasicWidget
	{
		public var tabs:Array;
		public var tabTitles:Array;
		public var currentTabIndex:int;
		private var tabTitleContainer:Sprite;
		private var tabContainer:Sprite;
		private var closeButton:BasicButton;
		private var tabTitlePosX:Number;
		private var tabTitlePosY:Number;
		private var tabPagePosX:Number;
		private var tabPagePosY:Number;
		private var titleType:int;
				     
		public function TabController(BackgroundImage:Class = null, CloseButtonImage:Class = null)
		{
			super();
			this.anchor = BasicWidget.ANCHOR_MIDDLE_MIDDLE;
			tabs = new Array();
			tabTitles = new Array();
			if (BackgroundImage != null)
			{
				var backgroundImage:Sprite = new BackgroundImage();
				addChild(backgroundImage);
			}
			tabTitleContainer = new Sprite();
			addChild(tabTitleContainer);
			tabContainer = new Sprite();
			addChild(tabContainer);
			if (CloseButtonImage != null)
			{
				//关闭按钮
				closeButton = new BasicButton(CloseButtonImage);
				closeButton.x = this.width - closeButton.width - 38;
				closeButton.y = 10;
				addChild(closeButton);
				closeButton.addEventListener(MouseEvent.CLICK, onCloseButtonClick);
			}
		}
		
		public function setTitleType(type:int):void
		{
			this.titleType = type;
		}
		
		public function setCloseButtonPos(posX:Number, posY:Number):void
		{
			if (closeButton != null)
			{
				closeButton.x = posX;
				closeButton.y = posY;
			}
		}
		
		public function setTabTitlePos(posX:Number, posY:Number, gap:int = 105):void
		{
			this.tabTitlePosX = posX;
			this.tabTitlePosY = posY;
			for (var i:int = 0; i < tabTitles.length; i++)
			{
				tabTitles[i].x = tabTitlePosX + i * gap;
				tabTitles[i].y = tabTitlePosY;
			}
		}
		
		public function setTabPagePos(posX:int, posY:int):void
		{
			this.tabPagePosX = posX;
			this.tabPagePosY = posY;
			for (var i:int = 0; i < tabs.length; i++)
			{
				tabs[i].x = tabPagePosX;
				tabs[i].y = tabPagePosY;
			}
		}
		
		public function removeAllItems():void
		{
			for (var i:int = 0; i < tabs.length; i++)
			{
				(tabs[i] as Tab).removeAll();
			}
		}
		
		public function removeAllTabs():void
		{
			while (tabTitles.length > 0)
			{
				var tabTitle:Sprite = tabTitles.pop();
				var tab:Sprite = tabs.pop();
				removeChild(tabTitle);
				removeChild(tab);
			}
			currentTabIndex = 0;
		}
		
		public function addTab(TabTitleBackground:Class, tabTitleName:String, tab:Sprite, HighlightTabBgr:Class = null):void
		{
			var tabTitle:TabTitle;
			//标签
			if (titleType == 0)
			{
				tabTitle = new TabButton(TabTitleBackground, tabTitleName, tabs.length, HighlightTabBgr);
				tabTitle.x = tabTitlePosX + tabs.length * (tabTitle.width + 5);
				tabTitle.y = tabTitlePosY;
//				tabTitle.filters = [currentTabTileFilter];
				tabTitle.addEventListener(MouseEvent.CLICK, onMouseClick);
				tabTitle.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
				tabTitle.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
				tabTitles.push(tabTitle);
				tabTitleContainer.addChild(tabTitle);
			}
			else
			{
				tabTitle = new TabIcon(TabTitleBackground, tabTitleName, tabs.length);
				tabTitle.x = tabTitlePosX + tabs.length * (tabTitle.width + 5);
				tabTitle.y = tabTitlePosY;
				tabTitle.addEventListener(MouseEvent.CLICK, onMouseClick);
				tabTitles.push(tabTitle);
				tabTitleContainer.addChild(tabTitle);
			}
			//标签页
			tab.x = tabPagePosX;
			tab.y = tabPagePosY;
			if (tabs.length != currentTabIndex)
			{
//				tab.visible = false;
			}
			else
			{
				tabContainer.addChild(tab);
				if (titleType == 0)
					tabTitle.highlight = true;
			}
			tabs.push(tab);
//			addChild(tab);
		}
		
		private function onMouseClick(event:MouseEvent):void
		{
			var clickedTabTitle:TabTitle = event.currentTarget as TabTitle;
			selectTab(clickedTabTitle.index);
		}
		
		public function selectTab(tabIndex:int):void
		{
			if (tabIndex != currentTabIndex)
			{
				if (tabs[currentTabIndex].parent != null)
					tabContainer.removeChild(tabs[currentTabIndex]);
				tabContainer.addChild(tabs[tabIndex]);
				(tabTitles[currentTabIndex] as TabTitle).highlight = false;
				(tabTitles[tabIndex] as TabTitle).highlight = true;
				(tabTitles[tabIndex] as TabTitle).filters = null;
				currentTabIndex = tabIndex;
			}
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			var tabTitle:TabTitle = event.currentTarget as TabTitle;
			if (tabTitle.index != currentTabIndex)
			{
				tabTitle.filters = [new ColorMatrixFilter(BFont.brightFilter)];
			}
			Mouse.cursor = "button";
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			var tabTitle:TabTitle = event.currentTarget as TabTitle;
			if (tabTitle.index != currentTabIndex)
			{
				tabTitle.filters = null;
			}
			Mouse.cursor = "auto";
		}

		private function onCloseButtonClick(event:MouseEvent):void
		{
			close();
		}

	}
}